package com.project.smarthome.login.view;


import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.project.smarthome.R;
import com.project.smarthome.base.BaseActivity;
import com.project.smarthome.base.NoUserAuthorize;
import com.project.smarthome.common.util.NetworkUtil;
import com.project.smarthome.common.util.UIUtils;
import com.project.smarthome.common.widget.AutoButton;
import com.project.smarthome.common.widget.AutoImageView;
import com.project.smarthome.common.widget.AutoTextView;
import com.project.smarthome.common.widget.SosDialog;
import com.project.smarthome.login.RegistContract;
import com.project.smarthome.login.presenter.RegistPresenter;
import com.project.smarthome.main.view.MainActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class RegistActivity extends BaseActivity implements RegistContract.View, NoUserAuthorize {

    @BindView(R.id.img_action_left)
    AutoImageView mImgActionLeft;
    @BindView(R.id.txt_action_title)
    TextView mTxtActionTitle;
    @BindView(R.id.img_action_right)
    AutoImageView mImgActionRight;
    @BindView(R.id.txt_right)
    AutoTextView mTxtRight;
    @BindView(R.id.title)
    RelativeLayout mTitle;
    @BindView(R.id.edit_phone)
    EditText mEditPhone;
    @BindView(R.id.edit_code)
    EditText mEditCode;
    @BindView(R.id.btn_code)
    AutoButton mBtnCode;
    @BindView(R.id.edit_pwd)
    EditText mEditPwd;
    @BindView(R.id.btn_regist)
    AutoButton mBtnRegist;
    @BindView(R.id.cb_agreement)
    CheckBox mCbAgreement;
    @BindView(R.id.txt_agreement)
    TextView mTxtAgreement;

    private Unbinder mUnbinder;
    private RegistPresenter mPresenter;

    @Override
    protected void initVariable() {
        mPresenter = new RegistPresenter(this);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_register);
        mUnbinder = ButterKnife.bind(this);
        initTitle(UIUtils.getString(R.string.register), R.drawable.select_return);
    }

    @Override
    protected void loadData() {
        mPresenter.init();
    }

    @OnClick({R.id.img_action_left, R.id.btn_code, R.id.btn_regist})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_action_left:
                exit();
                break;
            case R.id.btn_code:
                if (!NetworkUtil.hasNetwork(this)) {
                    showLoginError("不能连接服务器");
                    return;
                }
                mPresenter.getVerCode(mEditPhone.getText().toString().trim());
                break;
            case R.id.btn_regist:
                if (!NetworkUtil.hasNetwork(this)) {
                    showLoginError("不能连接服务器");
                    return;
                }
                mPresenter.toRegist(mEditPhone.getText().toString().trim(),
                        mEditPwd.getText().toString(), mEditCode.getText().toString().trim());
                break;
        }
    }

    @Override
    public void showCoutDownTime(long second) {  // #6788 java.lang.NullPointerException
        if (mBtnCode != null) {
            mBtnCode.setClickable(false);
            mBtnCode.setText(second + "秒后刷新");
        }
    }

    @Override
    public void showFinish() {
        if (mBtnCode == null) {
            return;
        }
        mBtnCode.setClickable(true);
        mBtnCode.setText(getString(R.string.register_getVerCode));
    }

    @Override
    public void showLoginError(String errmsg) {
        SosDialog dialog = new SosDialog(this);
        dialog.getDialog(this, errmsg, "知道了");
    }

    @Override
    public void loginSuccessToMain() {
        startActivity(this, MainActivity.class);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
        mPresenter.destroy();
        mPresenter = null;
    }
}
